home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
gnu
/
gcctest
/
tests05.zoo
/
tstdio.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-02
|
18KB
|
965 lines
/* E x e r c i s e
*
* This program exercises the stdio routines. It does not validate the
* routines but provides a convenient way to quickly check the functionality
* of the code.
*/
#ifdef _BSD
# include <strings.h>
# define remove unlink
# define sleep(x) usleep((x)*500)
#else
# include <stdlib.h>
# include <string.h>
#endif
#include <stdio.h>
#ifdef atarist
#include <memory.h>
#include <unistd.h>
#endif
#ifndef SEEK_SET
/* lseek() origins */
#define SEEK_SET 0 /* from beginning of file */
#define SEEK_CUR 1 /* from current location */
#define SEEK_END 2 /* from end of file */
#endif
#define UCHAR(x) ((int) ((x) & 0xff))
#define DEC -123
#define INT 255
#define UNS (~0)
#define TESTFILE "test.dat"
#define LARGEBUFS 16
#ifdef MSDOS
# define TTY "con"
#else
# ifdef atarist
# define TTY "CON:"
# else
# define TTY "/dev/tty"
# endif
#endif
#ifndef atarist
extern void *malloc(); /* memory allocator */
extern char *strcpy(); /* string copy */
extern char *strcat(); /* string concatenation */
extern int strcmp(); /* string compare */
extern void exit(); /* exit */
#endif
FILE *fp; /* per test file pointer */
/*
* Line Buffered Write Test
*
* Write to a terminal. This tests that the output buffer is
* flushed on receipt of a \n.
*/
void lbw_test()
{
int i;
puts("\nLine buffered write test");
#ifdef atarist
if ((fp = fopen(TTY, "wt")) != NULL) {
#else
if ((fp = fopen(TTY, "w")) != NULL) {
#endif
puts("<pause>ABCDEFGH");
puts("<pause>ABCD<pause>EFGH");
for (i = 0; i < 8; i++)
putc('A'+i, fp), sleep(1);
putc('\n', fp);
for (i = 0; i < 8; i++) {
putc('A'+i, fp);
if (i == 3)
fflush(fp);
sleep(1);
}
fclose(fp);
puts("");
}
}
/*
* Unbuffered Write Test
*
* Test that characters are written directly to the output device
* when the stream is unbuffered.
*/
void ubw_test()
{
int i;
puts("\nUnbuffered write test");
#ifdef atarist
if ((fp = fopen(TTY, "wt")) != NULL) {
#else
if ((fp = fopen(TTY, "w")) != NULL) {
#endif
setbuf(fp, (char *) 0);
puts("A<pause>B<pause>C<pause>D<pause>E<pause>F<pause>G<pause>H<pause>");
puts("A<pause>B<pause>C<pause>D<pause>E<pause>F<pause>G<pause>H<pause>");
for (i = 0; i < 8; i++)
putc('A'+i, fp), sleep(1);
putc('\n', fp);
for (i = 0; i < 8; i++)
putc('A'+i, fp), sleep(1);
fclose(fp);
puts("");
}
}
/*
* Buffered Write Test
*
* Test that the data is written to the terminal on a per buffer
* basis.
*/
void bw_test()
{
int i;
puts("\nFully buffered write test");
#ifdef atarist
if ((fp = fopen(TTY, "wt")) != NULL) {
#else
if ((fp = fopen(TTY, "w")) != NULL) {
#endif
setvbuf(fp, (char *) 0, _IOFBF, 4);
puts("<pause>ABCD<pause>EFGH<pause>");
#ifdef atarist
/* in text mode when we putc('\n', fp) we add a '\r' so we are off
by one char
*/
puts("AB<pause>CDEF<pause>GH<pause>");
#else
puts("ABC<pause>DEFG<pause>H<pause>");
#endif
for (i = 0; i < 8; i++)
putc('A'+i, fp), sleep(1);
putc('\n', fp);
for (i = 0; i < 8; i++)
putc('A'+i, fp), sleep(1);
fclose(fp);
sleep(2);
puts("");
}
}
/* Formatted Output Test
*
* This exercises the output formatting code.
*/
void fp_test()
{
int i, j, k, l;
char buf[7];
char *prefix = buf;
char tp[20];
puts("\nFormatted output test");
printf("prefix 6d 6o 6x 6X 6u\n");
strcpy(prefix, "%");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
for (k = 0; k < 2; k++) {
for (l = 0; l < 2; l++) {
strcpy(prefix, "%");
if (i == 0) strcat(prefix, "-");
if (j == 0) strcat(prefix, "+");
if (k == 0) strcat(prefix, "#");
if (l == 0) strcat(prefix, "0");
printf("%5s |", prefix);
strcpy(tp, prefix);
strcat(tp, "6d |");
printf(tp, DEC);
strcpy(tp, prefix);
strcat(tp, "6o |");
printf(tp, INT);
strcpy(tp, prefix);
strcat(tp, "6x |");
printf(tp, INT);
strcpy(tp, prefix);
strcat(tp, "6X |");
printf(tp, INT);
strcpy(tp, prefix);
strcat(tp, "6u |");
printf(tp, UNS);
printf("\n");
}
}
}
}
}
/*
* String Output Test
*
* Test the string printf code.
*/
void sw_test()
{
int i;
char buf[80];
puts("\nTest sprintf functionality");
puts("13 bytes in 'Testing 1 2 3'");
i = sprintf(buf, "Testing %d %d %d", 1, 2, 3);
printf("%d bytes in '%s'\n", i, buf);
}
/*
* String Input Test
*
* Test the string scanf code.
*/
void sr_test()
{
int i, j;
char buf[80];
puts("\nTest sscanf functionality");
puts("2 items yielding 25 and 'thompson'");
i = sscanf("25 thompson", "%d%s", &j, buf);
printf("%d items yielding %d and '%s'\n", i, j, buf);
}
/*
* File Write and Read Test
*
* Test that a file can be written to and read from.
*/
void frw_test()
{
int i, j, k;
char buf[80];
puts("\nFile write and read check");
#ifdef atarist
if ((fp = fopen(TESTFILE, "wt")) != NULL) {
#else
if ((fp = fopen(TESTFILE, "w")) != NULL) {
#endif
puts("3 items yielding 56, 789 and '56'");
puts("1 item yielding 'a72'");
fprintf(fp, "56789 0123 56a72");
#ifdef atarist
if (freopen(TESTFILE, "rt", fp) != fp)
#else
if (freopen(TESTFILE, "r", fp) != fp)
#endif
puts("Cannot open file for reading");
else {
i = fscanf(fp, "%2d%d%*d %[0-9]", &j, &k, buf);
printf("%d items yielding %d, %d and '%s'\n", i, j, k, buf);
i = fscanf(fp, "%s", buf);
printf("%d item yielding '%s'\n", i, buf);
fclose(fp);
}
}
}
/*
* File Seek Test
*
* Test that seek operations within files work.
*/
void fs_test()
{
int i, j;
puts("\nFile seek test");
#ifdef atarist
if ((fp = fopen(TESTFILE, "wb")) != NULL) {
#else
if ((fp = fopen(TESTFILE, "w")) != NULL) {
#endif
for (i = 0; i < 256; i++)
putc(i, fp);
#ifdef atarist
if (freopen(TESTFILE, "rb", fp) != fp)
#else
if (freopen(TESTFILE, "r", fp) != fp)
#endif
puts("Cannot open file for reading");
else {
for (i = 1; i <= 255; i++) {
printf("\r%3d ", i);
fflush(stdout);
fseek(fp, (long) -i, SEEK_END);
if ((j = getc(fp)) != 256-i) {
printf("SEEK_END failed %d\n", j);
break;
}
if (fseek(fp, (long) i, SEEK_SET)) {
puts("Cannot SEEK_SET");
break;
}
if ((j = getc(fp)) != i) {
printf("SEEK_SET failed %d\n", j);
break;
}
if (fseek(fp, (long) i, SEEK_SET)) {
puts("Cannot SEEK_SET");
break;
}
if (fseek(fp, (long) (i >= 128 ? -128 : 128), SEEK_CUR)) {
puts("Cannot SEEK_CUR");
break;
}
if ((j = getc(fp)) != (i >= 128 ? i-128 : i+128)) {
printf("SEEK_CUR failed %d\n", j);
break;
}
}
if (i > 255)
puts("ok");
fclose(fp);
}
}
}
/*
* Test gets()
*
* Checks that gets() works.
*/
void gets_test()
{
char buf[80];
puts("\nGets functionality");
puts("... Type a line and have it echoed ...");
gets(buf);
puts(buf);
}
/*
* Fputs Test
*
* Check that fputs() works into unbuffered streams.
*/
void fputs_test()
{
puts("\nUnbuffered fputs test");
#ifdef atarist
if ((fp = fopen(TTY, "wt")) != NULL) {
#else
if ((fp = fopen(TTY, "w")) != NULL) {
#endif
setbuf(fp, (char *) 0);
puts("ABCDEFGH<pause>");
puts("ABCDEFGH<pause>");
fputs("ABCDEFGH", fp), sleep(1);
fputs("\nABCDEFGH", fp), sleep(1);
fclose(fp);
puts("");
}
}
/*
* Fprintf Test
*
* Check that fprintf() works into unbuffered streams.
*/
void fprint_test()
{
puts("\nUnbuffered fprintf test");
#ifdef atarist
if ((fp = fopen(TTY, "wt")) != NULL) {
#else
if ((fp = fopen(TTY, "w")) != NULL) {
#endif
setbuf(fp, (char *) 0);
puts("ABCDEFGH<pause>");
puts("ABCDEFGH<pause>");
fprintf(fp, "ABCDEFGH"), sleep(1);
fprintf(fp, "\nABCDEFGH"), sleep(1);
fclose(fp);
puts("");
}
}
/*
* Fgets Test
*
* Check that fgets() works.
*/
void fgets_test()
{
char buf[80];
puts("\nFgets functionality");
puts("a");
puts("<pause>ab");
puts("<pause>abc");
puts("<pause>abcd");
puts("<pause>abcde");
puts("<pause>abcdef");
puts("<pause>abcdefg<pause>");
puts("<pause>abcdefg<pause>h");
#ifdef atarist